import utils from '../../../utils/util.js'
import { address } from '../../../api/eapi.js'

var   App=getApp();

const fNames = [
  'userName',
  'postalCode',
  'provinceName',
  'cityName',
  'countyName',
  'detailInfo',
  'telNumber'
  ]

Page({
  data: {
    region: ['省份', '城市', '区县'],
    form: {
      userName: '',
      postalCode: '',
      detailInfo: '',
      telNumber: ''
    },
    focus: {
      userName: false,
      postalCode: false,
      detailInfo: false,
      telNumber: false
    },
    inputValue:null
  },
  bindRegionChange(e) {
		console.log(e);
		this.setData({region: e.detail.value})
  },
  submit(e) {
    let finished = true;
    let fomatRegion = this.data.region.map((item) => {
      if(item === `省份` || item === `城市` || item === `区县`) {
         return ``;
      }else {
        return item
      }
    })

    let finalForm = Object.assign({}, this.data.form, {provinceName: fomatRegion[0], cityName: fomatRegion[1], countyName: fomatRegion[2]})

    fNames.forEach(item => {
      if(utils.isEmpty(finalForm[item])) finished = false;
    });
    if(!finished) {
      wx.showToast({title: '需要填写完整地址信息', icon: 'none'});
    }else {
      if(this.data.inputValue){
        address.update(finalForm,this.data.inputValue.id).then(res =>{
          wx.showToast({ title: `修改成功`, icon: 'none' });
          wx.setStorage({
            key: `address_change`,
            data: `true`
          })
          wx.setStorage({
            key: `me_address_change`,
            data: `true`
          })
          setTimeout(() => wx.navigateBack(),1000) 
        })
       }else{
      address.post(finalForm).then( resp => {
        wx.showToast({title: `添加成功`, icon: 'none'});
        wx.setStorage({
          key: `address_change`,
          data: `true`
        })
        wx.setStorage({
          key: `me_address_change`,
          data: `true`
        })
        wx.navigateBack()
      })
      }
    }
  },
  inputHander(e) {
    console.log(e);
  },
  inputBlur(e) {
    let fname = e.target.dataset.type;
    let value = e.detail.value;
    switch(fname) {
      case 'userName':
      this.setData({form: Object.assign({}, this.data.form, {userName: value})});
      break;
      case 'postalCode':
      this.setData({form: Object.assign({}, this.data.form, {postalCode: value})});
      break;
      case 'detailInfo':
      this.setData({form: Object.assign({}, this.data.form, {detailInfo: value})});
      break;
      case 'telNumber':

      if(utils.isPoneAvailable(value)) {
        this.setData({form: Object.assign({}, this.data.form, {telNumber: value})});
      }else {
        wx.showToast({title: `不是有效的电话号码`, icon:`none`});
        this.setData({focus: Object.assign({}, this.data.focus, {telNumber: true})});
      }

      break;
      default: ;
    }
    console.log(e);
  },
  onLoad:function(options){
    let ID=options.location;
    console.log(ID)
    if(ID){
      address.get().then(res=>{

        this.setData({
          inputValue:Object.assign({},res.data.filter(item=> item.id == ID)[0])
        }, () => {
          let data = this.data.inputValue; let location = [data.provinceName, data.cityName, data.countyName];
          let form=this.data.form;

          this.setData({
            region: location,
            form:Object.assign({},form,data)
          });
                    })
        
        
      })
    };

  }
})
